<template>
	<view class="main">
		<view class="login-top">
			<image src="http://xgr.oss-cn-hangzhou.aliyuncs.com/icon/icon/logo.png"></image>
		</view>
		<view class="v-box6">
			<block v-if="istel==false">
			<view class="xieyi dis-row">
				<view class="checkbox" style="padding-top: 13rpx;">
					<checkbox-group @change="checkRideo">
						<label style="padding-right: 0;">
							<checkbox style="transform:scale(0.7)" value="cn" checked="true"   />
						</label>				
					</checkbox-group>
				</view>
				同意
				<text @click="Userxieyi">《用户协议》</text>和
				<text @click="Useryinsi">《隐私协议》</text>
			</view>
			<view class="login-btn">
				<button style=" background: #0575FF; color: #fff;" plain="true" @click="weixinLogin">微信一键授权登录</button>
			</view>
			</block>
			<block v-if="istel==true">
				<button style=" background: #0575FF; color: #fff; font-size: 28rpx;" 
				open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" class="ft1">授权获取手机号码</button>
			</block>
		</view>
		
	</view>
</template>

<script>
import Index from "@/common/index-model.js"
let index = new Index()
let that = null
export default {
	data() {
		return {
			istel:false,
			value:'',
			loginFrom:{
				// phone:'18066742510',
				phone:'',
				code:''
			},
			logBtn: true,
			codemsg:'获取验证码',
			sendDataShow: true,
			sendData: 60,
			phonecheckmsg:'',
			isphone:true,
			iscode:true,
			codecheckmsg:'',
			modetel:'',
			wxback:{
				code:'',
				type:'',
				uid:'',
				checkCode:''
				
			},
			isgreen:true
		}
	},
	methods: {
		checkRideo(e) {
			if(e.detail.value.length == 0){
				this.isgreen = false
			}else{
				this.isgreen =  true
			}
		},
		Userxieyi() {
			index.navigate_to('./xieyi')
		},
		Useryinsi() {
			index.navigate_to('./yinsi')
		},
		getPhoneNumber (e) {
			this.wxback.code=e.detail.code;
			index.getPublicMember(this.wxback,'/client/third/auth/bindMiniMobile','post',res=>{
				index.show_tips(res.data.message)
				uni.setStorageSync('token',res.data.result.token);
				this.getHxUser();
				setTimeout(()=>{
					index.switch_tab('../../pages/index/index')
				},2000)
			})
		},
		closeTelTime() {
			this.istel = false
		},
		weixinLogin() {
			uni.showLoading({
				title: '登录中,请稍后'
			});
            if(this.isgreen == false){
				index.show_tips('请先同意用户协议和隐私协议')
				return false
			}
			uni.getUserProfile({
			   provider:'weixinlogin',
			   desc:"小程序授权登录",
			   success: (wx) => {
				   let tempUser={
					   'encryptedData':wx.encryptedData,
					   'userinfo':wx.userInfo
				   }
				   uni.login({
				   	provider:'tianxiagognren',
					success: (wxres) => {
						let sendpost={
							"code":wxres.code,
							"type":"WECHAT_MINI"
						}
						this.wxback.code = wxres.code
						this.wxback.type = "WECHAT_MINI"
						index.getPublicMember(sendpost,'client/third/auth/loginByCode','post',res=>{
							// this.istel = true
							// return false
							if(res.data.code == 200 && res.data.success){
								index.show_tips(res.data.message)
								uni.setStorageSync('token',res.data.result.token);
								this.getHxUser();
								setTimeout(()=>{
									index.switch_tab('../../pages/index/index')
								},2000)
							}else{
								if(res.data.code == '400000'){
									this.wxback.uid = res.data.result.uid
									this.wxback.checkCode = res.data.result.checkCode
									this.istel=true;
									uni.hideLoading();
									//index.redirectto('./wxlogin?uid='+res.data.result.uid+"&checkCode="+res.data.result.checkCode);
								}else{
									index.show_tips(res.data.message)
									return false
								}
							}
						})
						
					},
					fail: (err) => {
						console.log(err,'===登录失败=====')
					}
				   }) 
			   }
			})
		},
		closeTelTimeSure() {
			let send ={
				"mobile":this.modetel,
				"uid":this.wxback.uid,
				"checkCode":this.wxback.checkCode,
				"type":this.wxback.type
			}
			if(this.isgreen == false){
				index.show_tips('请先同意用户协议和隐私协议')
				return false
			}
			index.getPublicMember(send,'client/third/auth/bindMobile','post',res=>{
				if(res.data.code == 200 && res.data.success){
					index.show_tips(res.data.message)
					uni.setStorageSync('token',res.data.result.token);
					//uni.setStorageSync('userid',res.data.result.token);
					this.getHxUser();
					//uni.navigateBack();
					setTimeout(()=>{
						index.switch_tab('../../pages/index/index')
					},2000)
				}else{
					index.show_tips(res.data.message)
					return false
				}
			})
		},
		codeEnd(e){
			let codes=this.loginFrom.code;
			if(index.is_code_number(codes) && index.is_phone_number(this.loginFrom.phone)){
				this.logBtn=false;
			}else{
				this.logBtn=true;
			}
		},
		sendlogin() {
			if(!index.is_phone_number(this.loginFrom.phone)){
				this.phonecheckmsg = '请输入正确的手机号码'
				this.isphone=false
			}else{
				this.isphone=true
			}
			if(!index.is_code_number(this.loginFrom.code)){
				this.codecheckmsg = '请输入正确的验证码'
				this.iscode=false
			}else{
				this.iscode=true
			}
			uni.showLoading({
				title: '登录中,请稍后'
			});
			let sendPost = {
				"mobile":this.loginFrom.phone,
				"code": this.loginFrom.code
			}
			index.getPublicMember(sendPost,'/sys/mobileLogin','post',res=>{
				if(res.data.code==200){
					index.show_tips(res.data.message)
					uni.setStorageSync('token',res.data.result.token);
					uni.setStorageSync('userid',res.data.result.userId);
					this.getHxUser();
					//uni.navigateBack();
					setTimeout(()=>{
						index.switch_tab('../../pages/index/index')
					},2000)
				}else{
					index.show_tips(res.data.message)
					return false
				}
			})
		},
		getHxUser(){
			index.getPublicUserMember({},'/client/public/select/user','get',res=>{
				console.log(res);
				if(res.data.code==200){
					this.loginHx(res.data.result);
				}
			})
		},
		loginHx(data){
			var options = {
			    user: data.hxname,
			    pwd: data.hxPassword_,
			    appKey: this.$WebIM.config.appkey,
			    success: function (res) {
					// getApp().globalData.conn.open({
					// 	user: data.hxname,
					// 	accessToken: res.access_token,
					// });
					uni.setStorageSync('hxuser',data.hxname);
					uni.setStorageSync('hxpass',data.hxPassword_);
					uni.setStorageSync('hxtoken',res.access_token);
			    },
			    error: function(){
					
			    }
			};
			this.$WebIM.conn.open(options);
		},
		getCode() {
			if(!index.is_phone_number(this.loginFrom.phone)){
				index.show_tips('请输入正确的手机号码')
				return
			}
			let sendCode = {
				"mobile":this.loginFrom.phone,
				"smsmode":0
			}
			index.getPublicMember(sendCode,'sys/clientSms','post',res=>{
				if(res.data.success){
					index.show_tips('发送成功')
					this.sendDataShow = this.sendDataShow==true?false:true;
					var sen = setInterval(()=>{
						if(this.sendData <= 0){
							this.sendData = 60
							this.sendDataShow = true
							clearInterval(sen)
						}
						this.sendData--
					},1000)
				}else{
					this.sendDataShow = true
					this.sendData = 60
					index.show_tips(res.data.message)
					return false
				}
			})
				
		}
	},
	onLoad() {
		
	}
	
}
</script>

<style>
@import url("@/static/css/publics.css");	
@import url("@/static/css/login.css");		
</style>